<template>
  <div class="app-container">
    <el-form :model="filterFormData" ref="form" size="small" :inline="true">
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          id="roleName"
          v-model="filterFormData.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 170px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="filterFormData.createTime"
          type="date"
          value-format="timestamp"
          placeholder="选择日期"
          style="width: 170px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetFilterForm"
          >重置</el-button
        >
      </el-form-item>
      <el-button
        style="float: right"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleAddNew"
        >新增</el-button
      >
    </el-form>

    <!-- table -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
    >
      <el-table-column
        prop="roleName"
        label="角色名称"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <a
            style="color: #ff000dff; font-size: 14px"
            @click="handleDel(scope.row)"
            >删除</a
          >
          <a
            style="margin-left: 14px; color: #1890ffff; font-size: 14px"
            @click="handleDetail(scope.row)"
            >角色权限</a
          >
          <a
            style="margin-left: 14px; color: #1890ffff; font-size: 14px"
            @click="handleEdit(scope.row)"
            >编辑</a
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListHttpRequest"
    />
    <!-- 弹窗 -->
    <AddEditRoleDialog
      :dialogData="addEditRoleDialogData"
      @dialogConfirm="getListHttpRequest"
    ></AddEditRoleDialog>
    <ControlDialog
      :dialogData="controlRoleDialogData"
      @dialogConfirm="dialogConfirm"
    ></ControlDialog>
  </div>
</template>

<script>
import AddEditRoleDialog from "./components/add-edit-role-dialog.vue";
import ControlDialog from "./components/control-dialog.vue";
//请求
import { getRolePowerTree, getRoleList } from "@/api/staff_role";

export default {
  components: {
    AddEditRoleDialog,
    ControlDialog,
  },
  data() {
    return {
      // 筛选表单数据
      filterFormData: {
        name: "",
        createTime: "",
      },
      // 列表数据
      list: [],
      listLoading: false,
      total: 0,
      listQuery: {
        pageNum: 1, //页码 默认1
        pageSize: 10, //页大小 默认10
      },
      rolePowerTreeData: null,
      addEditRoleDialogData: {
        title: "",
        show: false,
      },
      controlRoleDialogData: {
        title: "",
        show: false,
      },
    };
  },
  mounted() {
    this.getListHttpRequest();
    this.initRolePowerTree();
  },
  methods: {
    //获取角色权限树
    initRolePowerTree() {
      getRolePowerTree()
        .then((response) => {
          this.rolePowerTreeData = response.data.nodeList;
        })
        .catch(() => {});
    },
    //重置筛选条件
    resetFilterForm() {
      this.$refs.form.resetFields();
    },

    //根据筛选条件进行查询
    handleQuery() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        ...this.filterFormData,
      };
      this.getListHttpRequest();
    },

    //列表请求
    getListHttpRequest() {
      this.listLoading = true;
      getRoleList(this.listQuery)
        .then((response) => {
          this.total = response.data.total;
          this.list = response.data.list;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    //新增员工
    handleAddNew() {
      this.addEditRoleDialogData = {
        title: "新增角色",
        type: "ADD",
        show: true,
        treeData: this.rolePowerTreeData,
      };
    },
    //编辑员工
    handleEdit(rowData) {
      this.addEditRoleDialogData = {
        title: "编辑角色",
        type: "EDIT",
        show: true,
        treeData: this.rolePowerTreeData,
        ...rowData,
      };
    },
    //查看角色权限
    handleDetail(rowData) {
      this.controlRoleDialogData = {
        title: "角色权限 ",
        type: "DETAIL",
        show: true,
        treeData: this.rolePowerTreeData,
        ...rowData,
      };
    },
    //删除角色
    handleDel(rowData) {
      this.controlRoleDialogData = {
        title: "删除角色 ",
        type: "DEL",
        show: true,
        treeData: this.rolePowerTreeData,
        ...rowData,
      };
    },

    //操作成功的回调
    dialogConfirm(type) {
      if (
        type == "DEL" &&
        this.list.length == 1 &&
        this.listQuery.pageNum > 1
      ) {
        this.listQuery.pageNum -= 1;
      }
      this.getListHttpRequest();
    },
  },
};
</script>

<style></style>